package com.tinem.platform.web.gateway.filter;

import com.tinem.platform.module.pojo.co.GatewayHeadName;
import com.tinem.platform.module.starter.sdk.MessageCode;
import com.tinem.platform.web.gateway.util.GatewayUtil;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className ResponseHeadGatewayFilterFactory
 * @description TODO
 * @site 
 * @company 
 * @create 2020-07-03 10:59
 */
@Component
public class ResponseHeadGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {

    @Override
    public GatewayFilter apply(Object config) {
        return (exchange,chain) -> {
            Mono<Void> mono =  chain.filter(exchange).doOnSuccess((e)->{
                exchange.getResponse().getHeaders().set(GatewayHeadName.X_PLATFORM_GATEWAY_REQUEST_ID, GatewayUtil.getReq(exchange));
                exchange.getResponse().getHeaders().set(GatewayHeadName.X_PLATFORM_GATEWAY_RES_SUCCES,"true");
                exchange.getResponse().getHeaders().set(GatewayHeadName.X_PLATFORM_GATEWAY_RES_CODE, MessageCode.SUCCESS.name());
                exchange.getResponse().getHeaders().set(GatewayHeadName.X_PLATFORM_GATEWAY_RES_MESSAGE,"success");
                exchange.getResponse().getHeaders().set(GatewayHeadName.X_PLATFORM_GATEWAY_RES_TIMESTAMP,System.currentTimeMillis()+"");
            });
            return mono;
        };
    }
}
